package com.ruoyi.web.controller.home;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.SocialSecurity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Drawing;
import com.ruoyi.system.service.IDrawingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 抽签Controller
 * 
 * @author xiongpeng
 * @date 2024-12-29
 */
@RestController
@RequestMapping("/drawing/drawing")
public class DrawingController extends BaseController
{
    @Autowired
    private IDrawingService drawingService;

    /**
     * 查询抽签列表
     */
    @PreAuthorize("@ss.hasPermi('drawing:drawing:list')")
    @GetMapping("/list")
    public TableDataInfo list(Drawing drawing)
    {
        startPage();
        List<Drawing> list = drawingService.selectDrawingList(drawing);
        return getDataTable(list);
    }

    /**
     * 导出抽签列表
     */
    @PreAuthorize("@ss.hasPermi('drawing:drawing:export')")
    @Log(title = "抽签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Drawing drawing)
    {
        List<Drawing> list = drawingService.selectDrawingList(drawing);
        ExcelUtil<Drawing> util = new ExcelUtil<Drawing>(Drawing.class);
        util.exportExcel(response, list, "抽签数据");
    }

    /**
     * 获取抽签详细信息
     */
    @PreAuthorize("@ss.hasPermi('drawing:drawing:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(drawingService.selectDrawingById(id));
    }

    /**
     * 新增抽签
     */
    @PreAuthorize("@ss.hasPermi('drawing:drawing:add')")
    @Log(title = "抽签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Drawing drawing)
    {
        return toAjax(drawingService.insertDrawing(drawing));
    }

    /**
     * 修改抽签
     */
    @PreAuthorize("@ss.hasPermi('drawing:drawing:edit')")
    @Log(title = "抽签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Drawing drawing)
    {
        return toAjax(drawingService.updateDrawing(drawing));
    }

    /**
     * 删除抽签
     */
    @PreAuthorize("@ss.hasPermi('drawing:drawing:remove')")
    @Log(title = "抽签", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(drawingService.deleteDrawingByIds(ids));
    }

    /**
     * 下载导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Drawing> util = new ExcelUtil<Drawing>(Drawing.class);
        util.importTemplateExcel(response, "居民医保申请明细数据");
    }

    @Log(title = "导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('drawing:drawing:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Drawing> util = new ExcelUtil<Drawing>(Drawing.class);
        List<Drawing> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = drawingService.importUser(userList, updateSupport, operName);
        return success(message);
    }
}
